﻿Public Class CINOACLabelState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINOACLabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-I") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CINOACLabelState.DoExecute")
        Dim readText As String = task.ProcessText
        Dim orderCd As String = ""
        Dim sysItemCd As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_I_ITEM_CD)
        Dim uniqueCd As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_I_UNIQUE_CD)
        Dim isInMazda As Boolean = False
        Dim labelType As Integer = CCommon.LABEL_INOAC
        Dim inBoxQty As Integer = CCommon.From36To10(CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_I_QTY))

        If isInMazda Then
            orderCd = CCommon.trimMazdaShipOrderOrderCd(orderCd)
        End If

        ' ''DAO
        'Dim itemDAO As CItemDAO = CItemDAO.GetInstance()
        'Dim itemData As strctItemInfo = itemDAO.GetItemInfo(itemKey)
        'If itemData.MngNo <> "" Then
        '    sysItemCd = itemData.SysItemCd
        'End If

        ''読込み済みのラベルではないか確認（ユニークコード）N:アラート
        Dim uniqueCdInfo As CPDATask.strctUniqueCdInfo = Nothing
        uniqueCdInfo.labelType = CCommon.LABEL_DNC
        uniqueCdInfo.itemCd = sysItemCd
        uniqueCdInfo.uniqueCd = uniqueCd
        uniqueCdInfo.readDateTime = Now()
        If Not pdaTask.addUniqueCd(uniqueCdInfo) Then
            ''アラート
            Dim result As String = ""
            Dim prevWindowMode As EnumWindowMode
            Do While result <> "[OK]"
                If result = "" Then
                    pdaTask.Hardware.alert()
                    prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                    pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_015, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                    pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                    'Debug.WriteLine(CCommon.CNS_MSG_015)

                End If
                result = task.ReadTextWait(-1)
                'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
            Loop
            pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
        Else
            Dim isParent As Boolean = False

            If pdaTask.checkParentLabel(labelType) Then

                If pdaTask.chkOutQty() Then
                    ''入り数チェック
                    ''正常
                    isParent = True
                    If pdaTask.commitLabel() Then
                        ''正常
                    Else
                        ''error(通常ありえない）
                        pdaTask.addWindowLabelBox(labelType, 2)
                        pdaTask.rollbackLabel()
                        'エラー
                        pdaTask.Hardware.warning()
                        ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                        SyncLock pdaTask.m_SendLockObject
                            Try
                                pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                                pdaTask.C_FileCommon.SendTrxOrder()

                            Catch ex As Exception

                            End Try
                        End SyncLock

                        Dim result As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result <> "[OK]"
                            If result = "" Then
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            End If
                            result = task.ReadTextWait(-1)
                            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                        task.ErrorCd = "ER"
                        task.ProcessText = ""
                        nextState = GetNextState(task, GetType(CErrorState))
                        Return nextState

                    End If
                Else
                    ''入り数チェック
                    ''エラー
                    ''error
                    pdaTask.addWindowLabelBox(labelType, 2)
                    pdaTask.rollbackLabel()
                    'エラー
                    pdaTask.Hardware.warning()
                    ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                    SyncLock pdaTask.m_SendLockObject
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try
                    End SyncLock


                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_025, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                    task.ErrorCd = "ER"
                    task.ProcessText = ""
                    nextState = GetNextState(task, GetType(CErrorState))
                    Return nextState


                End If
            End If

            'Dim labelData As typActionLabelRecord = pdaTask.chkGroupCd(pdaTask.C_FileCommon.GetLabel(readText))
            Dim groupCd As String = ""
            'Dim inBoxQty As Integer = pdaTask.C_SHIPORDER.getInBoxQty("", sysItemCd)
            Dim orderQty As Integer = 0 'CCommon.From36To10(CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_D_ORDER_QTY))
            'Dim inBoxQty As Integer = pdaTask.C_SHIPORDER.getInBoxQty(orderCd, sysItemCd)
            Dim isFullBox As Boolean = True
            Dim LabelRnk As String
            Dim label As New CLabelData()

            'If labelData.GroupCd Is Nothing Or labelData.GroupCd = "" Then
            'Else
            '    groupCd = labelData.GroupCd
            '    inBoxQty = labelData.ActionQty
            '    If labelData.LabelState = "F" Then
            '        isFullBox = True
            '    Else
            '        ''端数
            '        isFullBox = False
            '    End If
            'End If
            If isParent Then
                LabelRnk = "P"
            Else
                LabelRnk = "C"
            End If

            label.Id = 0
            label.GroupCd = groupCd
            label.OrderCd = orderCd
            label.SysItemCd = sysItemCd
            label.InBoxQty = inBoxQty
            label.IsFullBox = isFullBox
            label.ReadText = readText
            label.LabelType = labelType
            label.LabelRnk = LabelRnk

            label.IsInMazda = isInMazda

            'If readText.EndsWith(CCommon.CNS_DELIMITER_LABEL_FRACTION) Then
            '    label.IsFullBox = False
            '    isFullBox = False
            'Else
            '    label.IsFullBox = True
            '    isFullBox = True
            'End If

            Dim ret As COrderData = pdaTask.C_SHIPORDER.ChkLabelData(label)
            If Not ret Is Nothing Then
                pdaTask.setPrevData(ret)
                'If CCommon.getIntLabelType(ret.OrderType) = CCommon.LABEL_MAZDA Then
                '    ret.OrderRecord.RowColor = CCommon.COLOR_BLUE
                'End If
                If isParent Then
                    ret.OrderRecord.ActionBoxQty += 1
                    If ret.OrderRecord.ActionOrderQty + inBoxQty <= ret.OrderRecord.OrderQty Then
                        ret.OrderRecord.ActionOrderQty += inBoxQty
                    Else
                        ret.OrderRecord.ActionOrderQty = ret.OrderRecord.OrderQty
                    End If
                    Dim idx As Integer = pdaTask.WindowManager.GetIndex(ret.OrderRecord)
                    pdaTask.WindowManager.SetCurrentIndex(idx)
                End If

                Dim labelState As CPDATask.strctNowLabelState = Nothing
                labelState.LabelType = labelType
                labelState.IsInMazda = isInMazda ''DNCラベル読込時、Mの場合はTrue
                labelState.GroupCd = groupCd
                labelState.SysItemCd = sysItemCd
                labelState.OrderCd = orderCd
                labelState.OrderQty = orderQty
                labelState.InBoxQty = inBoxQty
                labelState.IsFullBox = isFullBox
                labelState.ReadText = readText
                labelState.LabelRnk = LabelRnk
                labelState.Begin = True

                If pdaTask.addLabel(labelState, isParent) Then
                    ''正常
                    pdaTask.addGroupCd(groupCd)
                    If isParent And ret.OrderRecord.BoxQty = ret.OrderRecord.ActionBoxQty Then
                        pdaTask.shipWriteWindow2(ret.ReportCd)
                    End If

                    ''ここのコメントアウトをはずせば読んだ瞬間に非表示になる
                    'Dim AppManager As CApplicationManager = CApplicationManager.GetInstance
                    'Dim isInnerLabelCheck As Boolean = AppManager.GetAppSettings.IsInnerLabelCheck
                    'If Not isInnerLabelCheck And Not isInMazda And ret.OrderRecord.BoxQty = ret.OrderRecord.ActionBoxQty Then
                    '    ret.OrderRecord.Visible = False
                    'End If


                    If isInMazda Then
                        ''マツダラベル対象の場合、ブザーを鳴らす
                        pdaTask.Hardware.nextC()
                    End If

                Else
                    ''error
                    pdaTask.addWindowLabelBox(labelType, 2)
                    pdaTask.rollbackLabel()
                    'エラー
                    pdaTask.Hardware.warning()
                    ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                    SyncLock pdaTask.m_SendLockObject
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, ret.ReportCd, ret.RowNum, ret.OrderCd, ret.SysItemCd, ret.OrderQty))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try
                    End SyncLock

                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                    task.ErrorCd = "ER"
                    task.ProcessText = ""
                    nextState = GetNextState(task, GetType(CErrorState))
                    Return nextState

                End If

            Else
                ''error
                pdaTask.addWindowLabelBox(labelType, 2)
                pdaTask.rollbackLabel()
                'エラー
                pdaTask.Hardware.warning()
                ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                SyncLock pdaTask.m_SendLockObject
                    Try
                        pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, labelType, "", "", "", "", 0))
                        pdaTask.C_FileCommon.SendTrxOrder()

                    Catch ex As Exception

                    End Try
                End SyncLock

                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                task.ErrorCd = "ER"
                task.ProcessText = ""
                nextState = GetNextState(task, GetType(CErrorState))
                Return nextState

            End If


        End If

        ' ''読込み済みのラベルではないか確認（ユニークコード）N:アラート
        'Dim uniqueCdInfo As CPDATask.strctUniqueCdInfo = Nothing
        'uniqueCdInfo.labelType = CCommon.LABEL_INOAC
        'uniqueCdInfo.itemCd = sysItemCd
        'uniqueCdInfo.uniqueCd = uniqueCd
        'uniqueCdInfo.readDateTime = Now()
        'If Not pdaTask.addUniqueCd(uniqueCdInfo) Then
        '    ''アラート
        '    Dim result As String = ""
        '    Dim prevWindowMode As EnumWindowMode
        '    Do While result <> "[OK]"
        '        If result = "" Then
        '            pdaTask.Hardware.alert()
        '            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
        '            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_015, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
        '            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
        '            'Debug.WriteLine(CCommon.CNS_MSG_015)

        '        End If
        '        result = task.ReadTextWait(-1)
        '        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
        '    Loop
        '    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
        'Else

        '    Dim labelData As typActionLabelRecord = pdaTask.chkGroupCd(pdaTask.C_FileCommon.GetLabel(readText))
        '    Dim groupCd As String = ""
        '    Dim inBoxQty As Integer = pdaTask.C_SHIPORDER.getInBoxQty("", sysItemCd)
        '    Dim isFullBox As Boolean = False
        '    Dim label As New CLabelData()

        '    If labelData.GroupCd Is Nothing Or labelData.GroupCd = "" Then
        '    Else
        '        groupCd = labelData.GroupCd
        '        inBoxQty = labelData.ActionQty
        '        If labelData.LabelState = "F" Then
        '            ''端数
        '            isFullBox = False
        '        Else
        '            isFullBox = True
        '        End If
        '    End If

        '    label.Id = 0
        '    label.GroupCd = groupCd
        '    label.OrderCd = orderCd
        '    label.SysItemCd = sysItemCd
        '    label.InBoxQty = inBoxQty
        '    label.IsFullBox = isFullBox
        '    'If readText.EndsWith(CCommon.CNS_DELIMITER_LABEL_FRACTION) Then
        '    '    label.IsFullBox = False
        '    '    isFullBox = False
        '    'Else
        '    '    label.IsFullBox = True
        '    '    isFullBox = True
        '    'End If
        '    Dim ret As COrderData = pdaTask.C_SHIPORDER.AddLabelData(label)
        '    If Not ret Is Nothing Then
        '        pdaTask.addGroupCd(groupCd)

        '        Dim idx As Integer = pdaTask.WindowManager.GetIndex(ret.RowNum)
        '        pdaTask.C_SHIPORDER.WriteWindow()
        '        pdaTask.WindowManager.SetCurrentIndex(idx)
        '        If ret.OrderRecord.Visible Then
        '            pdaTask.shipWriteWindow(ret.ReportCd, ret.RowNum, 0, label, 1)
        '        Else
        '            pdaTask.shipWriteWindow(ret.ReportCd, ret.RowNum, 1, label, 1)
        '        End If
        '    Else
        '        'エラー
        '        pdaTask.Hardware.warning()
        '        ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

        '        pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, CCommon.LABEL_INOAC, "", "", "", "", 0))
        '        pdaTask.C_FileCommon.SendTrxOrder()

        '        Dim result As String = ""
        '        Dim prevWindowMode As EnumWindowMode
        '        Do While result <> "[OK]"
        '            If result = "" Then
        '                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
        '                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
        '                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
        '            End If
        '            result = task.ReadTextWait(-1)
        '            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
        '        Loop
        '        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



        '        task.ErrorCd = "ER"
        '        task.ProcessText = ""
        '        nextState = GetNextState(task, GetType(CErrorState))
        '        Return nextState
        '    End If


        '    ''現在対象となっている注文番号に対応した品番か確認（アイテムコード）N:エラー
        '    'Dim labelInfo As CPDATask.strctLabelInfo = Nothing
        '    'labelInfo.labelType = CCommon.LABEL_INOAC
        '    'labelInfo.itemCd = itemCd
        '    'labelInfo.orderCd = ""
        '    'labelInfo.readText = readText
        '    'If Not pdaTask.setLabel(labelInfo) Then
        '    '    'エラー
        '    '    pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

        '    '    task.ErrorCd = "ER"
        '    '    task.ProcessText = ""
        '    '    nextState = GetNextState(task, GetType(CErrorState))
        '    '    Return nextState
        '    'End If
        'End If

        task.ActionCd = "LABEL"
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINOACLabelState.DoExit")
        Return True
    End Function


End Class
